package com.itlong.cloud.sdk.service.impl;

import com.itlong.cloud.POJO.DTO.sdk.CloudTalkDeviceGetPageDTO;
import com.itlong.cloud.POJO.VO.sdk.CloudTalkDeviceListPageVO;
import com.itlong.cloud.app.dao.IAppDeviceDao;
import com.itlong.cloud.app.dao.IAppVisitorPassCodeRecordDao;
import com.itlong.cloud.app.service.IAppDeviceService;
import com.itlong.cloud.sdk.dao.ICloudTalkSDKDeviceInfoDao;
import com.itlong.cloud.sdk.service.ICloudTalkSDKDeviceInfoService;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      云对讲sdk服务接口实现类。
 * <desc/>
 *
 * @createDate 2018-05-17.
 */
@Service
public class CloudTalkSDKDeviceInfoServiceImpl implements ICloudTalkSDKDeviceInfoService {


    @Autowired
    ICloudTalkSDKDeviceInfoDao iCloudTalkSDKDeviceInfoDao;

    @Autowired
    IAppDeviceDao iAppDeviceDao;

    @Autowired
    IAppVisitorPassCodeRecordDao iAppVisitorPassCodeRecordDao;

    @Autowired
    IAppDeviceService iAppDeviceService;

    
    /**
     * <desc>
     *      获取项目下云对讲的设备列表。
     * <desc/>
     *
     * @param  pageDTO
     * @return
     * @author shengen.T
     * @createDate 2018-05-17
     */
    @Override
    public Page<CloudTalkDeviceListPageVO> getCloudTalkDeviceList(CloudTalkDeviceGetPageDTO pageDTO) throws Exception{
        if (StringUtils.isEmpty(pageDTO.getPageSize())){
            pageDTO.setPageSize("15");
        }
        if (StringUtils.isEmpty(pageDTO.getCurrentPage())){
            pageDTO.setCurrentPage("1");
        }
        Page<CloudTalkDeviceListPageVO> page = new Page<>();
        Map<String, Object> params = new HashMap<>();
        page.getPage().setCurrentPage(pageDTO.getCurrentPage());
        page.getPage().setPageSize(pageDTO.getPageSize());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        params.put("projectId", pageDTO.getProjectId());
        List<CloudTalkDeviceListPageVO> list = iCloudTalkSDKDeviceInfoDao.getCloudTalkDeviceList(params);
        List<CloudTalkDeviceListPageVO> returnList = new ArrayList<>();
        for(CloudTalkDeviceListPageVO deviceListPageVO : list){
            //linux版本需取设备sip账号 android则取灵云快智账号
            if(deviceListPageVO.getSystemType().equals("2")){
                String lykzAccount = iCloudTalkSDKDeviceInfoDao.getLykzAccount(deviceListPageVO.getDeviceUnique());
                deviceListPageVO.setSipAccount(lykzAccount);
            }
            returnList.add(deviceListPageVO);
        }
        Integer count = iCloudTalkSDKDeviceInfoDao.getCloudTalkDeviceListCount(params);
        page.setRows(list,count);
        return page;
    }
}
